package com.oocl.homework.crm.datastruct;


import com.oocl.homework.crm.model.User;

public class UserDaoImpl implements Executeable<User>{
	
	private final static  MyLinkedList<User> users;
	
	private static UserDaoImpl userDaoImpl;
	
	private UserDaoImpl(){
		
	}
	
	public static UserDaoImpl getInstance(){
		if(userDaoImpl==null){
			synchronized (UserDaoImpl.class) {
				if (userDaoImpl==null) {
					userDaoImpl = new UserDaoImpl();
				}
			}
		}
		return userDaoImpl;
	}
	
	static{
		users = new MyLinkedList<>();
			
		User user = new User(0,"Jimmy","male","2017/7/03 09:25:13","珠海市","12456378954");
		User user1 = new User(1,"harry","male","2016/7/03 09:25:13","重庆市","12456378954");
		User user2 = new User(2,"nico","female","2015/7/03 09:25:13","北京市","12456378954");
		User user3 = new User(3,"cody","male","2014/7/03 09:25:13","上海市","12456378954");
		User user4 = new User(4,"allen","male","2013/7/03 09:25:13","广州市","12456378954");
		User user5 = new User(5,"jason","male","2012/7/03 09:25:13","深圳市","12456378954");
		User user6 = new User(6,"James","male","2011/7/03 09:25:13","中山市","12456378954");
		User user7 = new User(7,"elvis","male","2010/7/03 09:25:13","成都市","12456378954");
		User user8 = new User(8,"tommy","male","2009/7/03 09:25:13","东莞市","12456378954");
		User user9 = new User(9,"tom","male","2008/7/03 09:25:13","东莞市","12456378954");
			
		users.add(user);
		users.add(user1);
		users.add(user2);
		users.add(user3);
		users.add(user4);
		users.add(user5);
		users.add(user6);
		users.add(user7);
		users.add(user8);
		users.add(user9);
		
	}
	
	public void add(User user){
		user.setId(users.get(users.getSize()-1).getId()+1);
		if (user.getAddress()==null) {
			System.out.println("adddress canot be null");
			return;
		}
		if (user.getName()==null) {
			System.out.println("name canot be null");
			return;
		}
		if (user.getBirthday()==null) {
			System.out.println("birthday canot be null");
			return;
		}
		if (user.getSex()==null) {
			System.out.println("sex canot be null");
			return;
		}
		if (user.getPhone()==null) {
			System.out.println("phone canot be null");
			return;
		}
		
		users.add(user);
	}
	
	
	public void delete(User user){
		int index = users.searchPosition(user);
		if (index ==-1) {
			System.out.println("record ont exist");
			return;
		}
		users.deleteWithIndex(index);
	}
	
	
	public String search(User user){
		if (user==null) {
			return users.toString();
		}
		return users.search(user);
	}
	
	public void update(User user){
		
		User user1 = users.getWithId(user);
		if(user1!=null){
			if (user.getAddress()==null) {
				user.setAddress(user1.getAddress());
			}
			if (user.getName()==null) {
				user.setName(user1.getName());
			}
			if (user.getBirthday()==null) {
				user.setBirthday(user1.getBirthday());
			}
			if (user.getSex()==null) {
				user.setSex(user1.getSex());
			}
			if (user.getPhone()==null) {
				user.setPhone(user1.getPhone());
			}
			users.replace(user);
		}
		
	
	}
	
	public String sortByName() throws Exception{
//		users.sortByName();
		User[] userList =new User[users.getSize()];
		for (int i = 0; i < userList.length; i++) {
			userList[i] = users.get(i);
		}
		for (int i = 0; i < userList.length; i++) {
			for (int j = 0; j < userList.length-i-1; j++) {
				if (userList[j].compareNameTo(userList[j+1])>0) {
					User temp = userList[j+1];
					userList[j+1] = userList[j];
					userList[j] = temp;
				}
			}
		}
		String result = "";
		for (int i = 0; i < userList.length; i++) {
			result+=userList[i].toString()+"\n";
		}
		return result;
	}
	
	public String sortById() throws Exception{
//		users.sortById();
		return users.toString();
	}


	
	

}
